<template>
  <el-container class="layout-page">
    <el-dialog
     title="提示"
     :visible.sync="model1"
     width="30%"
    >
     <span>修改头像</span>
     <el-upload
     class="avatar-uploader"
     action="http://localhost:8095/upload"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload">
    <img v-if="imageUrl" :src="imageUrl" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
     <span slot="footer" class="dialog-footer">
    <el-button @click="model1 = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">提 交</el-button>
    </span>
    </el-dialog>
    <el-aside width="200px">
      <div class="logo">管理后台</div>
      <el-menu
        router
        :default-active="$route.path"
        background-color="#313a46"
        text-color="#8391a2"
        active-text-color="#FFF"
      >
        <el-menu-item index="/dashboard">
          <i class="el-icon-pie-chart"></i>
          <span>数据看板</span>
        </el-menu-item>

        <el-menu-item index="/article">
          <i class="el-icon-notebook-1"></i>
          <span>文章管理</span>
        </el-menu-item>

      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div class="user" >
          <div v-on:click="openHead()">
         <el-avatar
         :size="36"
         :src="avatar"
         style="cursor: pointer"
         ></el-avatar>
          </div>
          
          <el-link :underline="false">{{name}}</el-link>  
        </div>
        <div class="logout">
          <el-popconfirm title="您确认退出吗？" @confirm = 'logout'>
            <i slot="reference" title="logout" class="el-icon-switch-button"></i>
          </el-popconfirm>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import requst from '@/utils/request'
export default {
  name: 'layout-page',
  data () {
    return {
      name: '',
      avatar: '',
      model1: false,
      imageUrl: ''
    }
  },
  methods: {
    //退出登录，删除token
    logout () {
      this.$store.commit("user/del")
      requst.post("http://localhost:8095/user/logout")
      this.$router.push("/login")
    },
    openHead () {
      this.model1 = true
      console.log(111);
    },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        console.log(this.imageUrl);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
  },
  mounted () {
    this.name = JSON.parse(localStorage.getItem("user")).userName
    this.avatar = JSON.parse(localStorage.getItem("user")).userHead
  }
  
}
</script>

<style lang="scss" scoped>
.layout-page {
  height: 100vh;
  .el-aside {
    background: #313a46;
    .logo {
      color: #fff;
      font-size: 20px;
      height: 60px;
      line-height: 60px;
      text-align: center;
    }
    .el-menu {
      border-right: none;
      margin-top: 20px;
      &-item {
        background-color: transparent !important;
        > span, i {
          padding-left: 5px;
        }
      }
    }
  }
  .el-header {
    box-shadow: 0px 0px 35px 0px rgba(154, 161, 171, 0.15);
    background: #fff;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    z-index: 999;
    .user {
      display: flex;
      align-items: center;
      background: #fafbfd;
      height: 60px;
      border: 1px solid #f1f3fa;
      padding: 0 15px;
      .el-avatar {
        margin-right: 15px;
      }
    }
    .logout {
      font-size: 20px;
      color: #999;
      cursor: pointer;
      padding: 0 15px;
    }
  }
  .el-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #aaa;
    border-top: 1px solid rgba(152, 166, 173, 0.2);
    font-size: 14px;
  }
   .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>

